package com.hzx.demo.activity;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.support.v7.app.AppCompatActivity;
import android.util.Size;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.io.IOException;
import java.util.List;

/**
 * Created by ldkxingzhe on 2016/5/23.
 */
@Entry(desc = "测试所有跟Camera类有关的api", createTime = "2016-05-23 09:17")
@EActivity(R.layout.activity_camera)
public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback {
    @SuppressWarnings("unused")
    private static final String TAG = "CameraActivity";

    @ViewById(R.id.sv_camera_preview)
    protected SurfaceView mSurfaceView;

    private Camera mCamera;

    @AfterViews
    protected void afterView(){
        mSurfaceView.getHolder().addCallback(this);
    }

    @Click(R.id.btn_click_me)
    protected void onClickMe(){
        HZXLog.v(TAG, "onClickMe");
        HZXLog.v(TAG, "the number of Camera is %d", Camera.getNumberOfCameras());
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        Camera.getCameraInfo(0, cameraInfo);
        HZXLog.v(TAG, "The camera's facing  is %s", cameraInfo.facing);
        mCamera = Camera.open(0);
        List<Camera.Size> previewSizeList = mCamera.getParameters().getSupportedPreviewSizes();
        StringBuilder builder = new StringBuilder();
        for(Camera.Size size : previewSizeList){
            builder.append(size.width).append("x").append(size.height);
            builder.append("\n");
        }
        HZXLog.v(TAG, "previewSizes is \n%s.", builder.toString());
        try {
            mCamera.setPreviewDisplay(mSurfaceView.getHolder());
            Camera.Parameters cameParameters = mCamera.getParameters();
            cameParameters.setPreviewSize(480, 640);
            HZXLog.v(TAG, "cameraParameters's pictureSize is %s x %s.", cameParameters.getPictureSize().width, cameParameters.getPictureSize().height);
            mCamera.setParameters(cameParameters);
            mCamera.setDisplayOrientation(90);
            mCamera.setOneShotPreviewCallback(this);
//            mCamera.setPreviewCallback(this);
            mCamera.startPreview();
        } catch (IOException e) {
            HZXLog.e(TAG, e.getMessage(), e);
        }
        camera2Test();
    }

    private void camera2Test(){
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        String cameraId = "0";
        CameraCharacteristics characteristics = null;
        try {
            characteristics = cameraManager.getCameraCharacteristics(cameraId);
            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);


            Size[] previewSize = map.getOutputSizes(SurfaceTexture.class);
            HZXLog.v(TAG, "previewSize is %d, and is %s", previewSize.length, previewSize);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        mCamera.release();
        mCamera = null;
        HZXLog.v(TAG, "onPause");
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        HZXLog.v(TAG, "surfaceCreated");
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        HZXLog.v(TAG, "surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        HZXLog.v(TAG, "surfaceDestroyed");
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        HZXLog.v(TAG, "onPreviewFrame, and data.length is %d.", data.length);
    }
}
